// const json5_parse = require('../client/common.js').json5_parse;

const json5 = require('json5');

module.exports = function(jsondiffpatch, formattersHtml, curDiffData) {
  const json5_parse = json => {
    if (typeof json === 'object' && json) return json;
    try {
      return json5.parse(json);
    } catch (err) {
      return json;
    }
  };

  const diffText = (left, right) => {
    left = left || '';
    right = right || '';
    if (left == right) {
      return null;
    }

    var delta = jsondiffpatch.diff(left, right);

    let result = formattersHtml.format(delta, left);

    return result;
  };

  const diffJson = (left, right) => {
    left = json5_parse(left);
    right = json5_parse(right);
    let delta = jsondiffpatch.diff(left, right);
    return formattersHtml.format(delta, left);
    // return '';
  };
  const statusMaps = {
    '0': '设计中',
    '1': 'sit测试',
    '2': 'uat测试',
    '3': '已上线'
  } ;

  const valueMaps = {
    '1': '必须',
    '0': '非必须',
    text: '文本',
    file: '文件',
    undone: '未完成',
    done: '已完成'
  };

  const handleParams = item => {
    let newItem = Object.assign({}, item);
    newItem._id = undefined;

    Object.keys(newItem).forEach(key => {
      switch (key) {
        case 'required':
          newItem[key] = valueMaps[newItem[key]];
          break;
        case 'type':
          newItem[key] = valueMaps[newItem[key]];
          break;
      }
    });
    return newItem;
  };

  const diffArray = (arr1, arr2) => {
    arr1 = arr1 || [];
    arr2 = arr2 || [];
    arr1 = arr1.map(handleParams);
    arr2 = arr2.map(handleParams);
    return diffJson(arr1, arr2);
  };

  let diffView = [];

  if (curDiffData && typeof curDiffData === 'object' && curDiffData.current) {
    const { current, old, type } = curDiffData;
    // 接口方法更新 diff 输出
    if (type === 'method_up') {
      if (current.chltype != old.chltype) {
        diffView.push({
          title: '渠道编号',
          content: diffText(old.path, current.path)
        });
      }
      if (current.cname != old.cname) {
        diffView.push({
          title: '方法名',
          content: diffText(old.cname, current.cname)
        });
      }

      if (current.inname != old.inname) {
        diffView.push({
          title: '请求类名',
          content: diffText(old.inname, current.inname)
        });
      }

      if (current.outname != old.outname) {
        diffView.push({
          title: '响应类名',
          content: diffText(old.outname, current.outname)
        });
      }

      if (current.status != old.status) {
        diffView.push({
          title: '接口状态',
          content: diffText(statusMaps[old.status], statusMaps[current.status])
        });
      }

      if (current.onlinedate !== old.onlinedate) {
        diffView.push({
          title: '上线时间',
          content: diffText(old.onlinedate, current.onlinedate)
        });
      }

      if (current.inbody !== old.inbody) {
        diffView.push({
          title: '请求字段',
          content: diffText(old.inbody, current.inbody)
        });
      }

      if (current.outbody !== old.outbody) {
        diffView.push({
          title: '响应字段',
          content: diffText(old.outbody, current.outbody)
        });
      }
      return (diffView = diffView.filter(item => item.content));
    }
      if (type === 'interface_up'){
          if (current.cname !== old.cname) {
              diffView.push({
                  title: '接口名称',
                  content: diffText(old.cname, current.cname)
              });
          }
          if (current.classname !== old.classname) {
              diffView.push({
                  title: '接口名称',
                  content: diffText(old.classname, current.classname)
              });
          }
          if (current.status != old.status) {
              diffView.push({
                  title: '接口状态',
                  content: diffText(statusMaps[old.status], statusMaps[current.status])
              });
          }
          if (current.onlinedate !== old.onlinedate) {
              diffView.push({
                  title: '上线时间',
                  content: diffText(old.onlinedate, current.onlinedate)
              });
          }
          if (current.desc !== old.desc) {
              diffView.push({
                  title: '接口描述',
                  content: diffText(old.desc, current.desc)
              });
          }
          return (diffView = diffView.filter(item => item.content));
      }


    let oldValue = current.req_body_type === 'form' ? old.req_body_form : old.req_body_other;
    if (current.req_body_type !== old.req_body_type) {
      diffView.push({
        title: 'Request Type',
        content: diffText(old.req_body_type, current.req_body_type)
      });
      oldValue = null;
    }

    if (current.req_body_type === 'json') {
      diffView.push({
        title: 'Request Body',
        content: diffJson(oldValue, current.req_body_other)
      });
    } else if (current.req_body_type === 'form') {
      diffView.push({
        title: 'Request Form Body',
        content: diffArray(oldValue, current.req_body_form)
      });
    } else {
      diffView.push({
        title: 'Request Raw Body',
        content: diffText(oldValue, current.req_body_other)
      });
    }

    let oldResValue = old.res_body;
    if (current.res_body_type !== old.res_body_type) {
      diffView.push({
        title: 'Response Type',
        content: diffText(old.res_body_type, current.res_body_type)
      });
      oldResValue = '';
    }

    if (current.res_body_type === 'json') {
      diffView.push({
        title: 'Response Body',
        content: diffJson(oldResValue, current.res_body)
      });
    } else {
      diffView.push({
        title: 'Response Body',
        content: diffText(oldResValue, current.res_body)
      });
    }
  }

  return (diffView = diffView.filter(item => item.content));
};
